package com.icss.ah.base.util;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author yanjsh
 *
 */
public class DateUtils {
	public static String getCurrentDate(String model){
		String date="";
		try{
			Date dt = new Date();
	        SimpleDateFormat formatter= new SimpleDateFormat(model);
	    	date = formatter.format(dt);
		}catch(Exception ex){
			date="";
		}
		return date;
	}	
    public static String getMonthByNum(String model,int i){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat df = new SimpleDateFormat(model);
        String s = df.format(calendar.getTime());
        return s;
    }
    public static String getMonthByNum(String dateStr,String model,int i){
        Calendar calendar = Calendar.getInstance();
        Date date = strToDate(dateStr,model);
        calendar.setTime(date);
        calendar.add(calendar.MONTH, i);
        SimpleDateFormat df = new SimpleDateFormat(model);
        String s = df.format(calendar.getTime());
        return s;
	}
    public static String getDayByNum(String dateStr,String model,int i){
        Calendar calendar = Calendar.getInstance();
        Date date = strToDate(dateStr,model);
        calendar.setTime(date);
        calendar.add(calendar.DATE, i);
        SimpleDateFormat df = new SimpleDateFormat(model);
        String s = df.format(calendar.getTime());
        return s;
	}
	public static String getDayByNum(String model,int i){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, i);
        SimpleDateFormat df = new SimpleDateFormat(model);
        String s = df.format(calendar.getTime());
        return s;
	}
	public static String getLastMonth(String bursarMonth,String model,int i){
		SimpleDateFormat sdNowDate = new SimpleDateFormat(model);
		Calendar cal = Calendar.getInstance();
		Date date = strToDate(bursarMonth,model);  
		cal.setTime(date);
		cal.add(cal.MONTH, i);
		return sdNowDate.format(cal.getTime()); 
	}
	public static String getLastMonthModel(String bursarMonth,String model1,String model2,int i){
		SimpleDateFormat sdNowDate = new SimpleDateFormat(model2);
		Calendar cal = Calendar.getInstance();
		Date date = strToDate(bursarMonth,model1);  
		cal.setTime(date);
		cal.add(cal.MONTH, i);
		return sdNowDate.format(cal.getTime()); 
	}
	public static Date strToDate(String strDate,String model) {   
        SimpleDateFormat formatter = new SimpleDateFormat(model);   
        ParsePosition pos = new ParsePosition(0);   
        Date strtodate = formatter.parse(strDate, pos);   
        return strtodate;   
    }
	public static void main(String[] args){
		System.out.println("--------------"+getLastMonthModel("201404","yyyyMM","yyyyMM",-1));
		System.out.println("--------------"+formatTime("20140425124532","yyyyMMddHHmmss"));
	}
	/*
	 * 格式化传入的日期/时间
	 */
	public static String formatTime(String time, String model){
		SimpleDateFormat formatterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat formatter = new SimpleDateFormat(model);
		String timeFormatter = "";
		try {
			timeFormatter = formatter.format(formatterTime.parse(time));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timeFormatter;
	}
	public static String formatTime(Date time, String model){
		SimpleDateFormat formatter = new SimpleDateFormat(model);
		String timeFormatter = "";
		try {
			timeFormatter = formatter.format(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timeFormatter;
	}
	public static String formatTime(Long time, String model){
		SimpleDateFormat formatter = new SimpleDateFormat(model);
		String timeFormatter = "";
		try {
			timeFormatter = formatter.format(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timeFormatter;
	}
	
	//获取当前月份
	public static String getBursarMonth(){
		DateFormat df = new SimpleDateFormat("yyyyMM");
		Date date = new Date();
		String bursarMonth = "";
		bursarMonth = df.format(date);
		return bursarMonth;
	}
	/**
	 * 当前月相隔月日期
	 * @param month
	 * @return
	 */
	public static String getBursarMonth(int month){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String bursarMonthBefore = "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.MONTH, month);
		bursarMonthBefore = df.format(cal.getTime());
		return bursarMonthBefore;
	}
	//获取上月月份
	public static String getBursarMonthBefore(){
		DateFormat df = new SimpleDateFormat("yyyyMM");
		Date date = new Date();
		String bursarMonthBefore = "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.MONTH, -1);
		bursarMonthBefore = df.format(cal.getTime());
		return bursarMonthBefore;
	}
	//获取1年前的时间
	public static String getTimeBeforeYear(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String timeBeforeYear = "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.MONTH, -12);
		timeBeforeYear = df.format(cal.getTime());
		return timeBeforeYear;
	}
	public final static String getCurDateString(String aMask) {
		return getDateString(new Date(), aMask);
	}
	public final static String getDateString(Date date, String aMask) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(aMask);
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "1900-01-01";
		}
	}

}